package org.hydrakyoufeng.stuplatform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.hydrakyoufeng.stuplatform.dto.*;
import org.hydrakyoufeng.stuplatform.mapper.CourseMapper;
import org.hydrakyoufeng.stuplatform.service.CourseService;
import org.hydrakyoufeng.stuplatform.utils.ResultEntity;
import org.hydrakyoufeng.stuplatform.vo.CourseListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    @Qualifier("adminIdTheadLocal")
    private ThreadLocal<Integer> adminIdTheadLocal;

    @Autowired
    @Qualifier("userIdTheadLocal")
    private ThreadLocal<Integer> userIdTheadLocal;

    /**
     * 导出excel功能，获取不分页的列表
     *
     * @param courseListDTO
     * @return
     */
    @Override
    public List<CourseListVO> getExcel(CourseListDTO courseListDTO) {
        return courseMapper.queryList(courseListDTO);
    }


    /**
     * 查询班级中已经分配的课程列表（不分页）
     *
     * @param classId
     * @return
     * @throws Exception
     */
    @Override
    @Cacheable(value = "course:inList",keyGenerator = "courseKeyGenerator")
    public ResultEntity queryListInClass(Integer classId) throws Exception {
        return ResultEntity.success("查询成功", courseMapper.queryListInClass(classId));
    }

    /**
     * 查询班级中未分配的课程列表（分页查询）
     *
     * @param courseListDTO
     * @return
     * @throws Exception
     */
    @Override
    @Cacheable(value = "course:notInList",keyGenerator = "courseKeyGenerator")
    public ResultEntity queryListNotInClass(CourseListDTO courseListDTO) throws Exception {
        PageHelper.startPage(courseListDTO.getCurrentPage(),
                courseListDTO.getPageSize(),
                "c.update_time desc");
        PageInfo pageInfo = new PageInfo(courseMapper.queryListNotInClass(courseListDTO));
        return ResultEntity.success("查询成功", pageInfo);
    }


    @Override
    @Cacheable(value = "course:list",keyGenerator = "courseKeyGenerator")
    public ResultEntity queryList(CourseListDTO courseListDTO) {
        // 开启分页
        PageHelper.startPage(courseListDTO.getCurrentPage(),
                courseListDTO.getPageSize(),
                "c.update_time desc");
        PageInfo pageInfo = new PageInfo(courseMapper.queryList(courseListDTO));
        return ResultEntity.success("查询成功", pageInfo);
    }

    @Override
    public ResultEntity insert(CourseDTO courseDTO) throws Exception {
        courseDTO.setCreator_by(adminIdTheadLocal.get());
        courseDTO.setUpdate_by(adminIdTheadLocal.get());
        courseDTO.setStatus(0);
        int result = courseMapper.insert(courseDTO);
        if (result > 0) {
            return ResultEntity.success("添加成功");
        } else {
            return ResultEntity.error("添加失败");
        }
    }

    @Override
    public ResultEntity update(CourseDTO courseDTO) throws Exception {
        courseDTO.setUpdate_by(adminIdTheadLocal.get());
        int result = courseMapper.update(courseDTO);
        if (result > 0) {
            return ResultEntity.success("修改成功");
        } else {
            return ResultEntity.error("修改失败");
        }
    }

    @Override
    public ResultEntity<Object> delete(Integer id) {
        CourseDTO courseDTO = new CourseDTO();
        courseDTO.setId(id);
        courseDTO.setUpdate_by(adminIdTheadLocal.get());
        int result = courseMapper.delete(courseDTO);
        if (result > 0) {
            return ResultEntity.success("删除成功");
        } else {
            return ResultEntity.error("删除失败");
        }
    }

    /**
     * 前端用户查询课程列表
     *
     * @param courseUserListDTO
     * @return
     */
    @Override
    public ResultEntity queryUserList(CourseUserListDTO courseUserListDTO) {
        courseUserListDTO.setUser_id(userIdTheadLocal.get());
        // 这里将就现有的逻辑做个判断，当用户没有设置分页大小时，不进行分页
        if (courseUserListDTO.getPageSize() < 0) {
            return ResultEntity.success("查询成功", courseMapper.queryUserList(courseUserListDTO));
        }
        // 开启分页
        PageHelper.startPage(courseUserListDTO.getCurrentPage(),
                courseUserListDTO.getPageSize(),
                "c.update_time desc");
        PageInfo pageInfo = new PageInfo(courseMapper.queryUserList(courseUserListDTO));
        return ResultEntity.success("查询成功", pageInfo);
    }

    /**
     * 更新用户的操作记录
     *
     * @param courseUserOperateDTO
     * @return
     */
    @Override
    public ResultEntity updateUserOperate(CourseUserOperateDTO courseUserOperateDTO) {
        courseUserOperateDTO.setUser_id(userIdTheadLocal.get());
        int count = courseMapper.queryOperateCount(courseUserOperateDTO);
        int effect = 0;
        // 没有查到：增加数据
        if (count == 0) {
            // 仅记录数据做insert操作
            if ("0".equals(courseUserOperateDTO.getStatus())) {
                effect = courseMapper.insertOperate(courseUserOperateDTO);
            }
            // 查询到数据：修改记录
        } else {
            effect = courseMapper.updateOperate(courseUserOperateDTO);
        }
        if (effect > 0) {
            return ResultEntity.success("操作成功");
        } else {
            return ResultEntity.error("操作失败");
        }
    }

    @Override
    public ResultEntity insertCorrecting(CorrectingUserDTO dto) {
        dto.setCreator_by(userIdTheadLocal.get());
        int effect = courseMapper.insertCorrecting(dto);
        if (effect > 0) {
            return ResultEntity.success("操作成功");
        } else {
            return ResultEntity.error("操作失败");
        }
    }

    @Override
    public ResultEntity queryCorrectingList(CorrectingListDTO dto) {
        // 开启分页
        PageHelper.startPage(dto.getCurrentPage(),
                dto.getPageSize(),
                "c.update_time desc");
        PageInfo pageInfo = new PageInfo(courseMapper.queryCorrectingList(dto));
        return ResultEntity.success("查询成功", pageInfo);
    }

    /**
     * 管理员审核纠错信息
     * @param dto
     * @return
     */
    @Override
    public ResultEntity updateCorrecting(CorrectingDTO dto) {
        dto.setUpdate_by(adminIdTheadLocal.get());
        int effect = courseMapper.updateCorrecting(dto);
        if (effect > 0) {
            return ResultEntity.success("操作成功");
        } else {
            return ResultEntity.error("操作失败");
        }
    }
}